import { Component, OnInit } from '@angular/core';
import { Http, XHRBackend, RequestOptions, Request, RequestOptionsArgs, Response } from "@angular/http";
import {LocalStorage} from "../../utils/utils";
import {FormBuilder,FormGroup,Validators} from '@angular/forms';
import {Router} from "@angular/router";
import {LoginService} from "../../services/services";
import {User} from "../../entities/entities";
/**登录界面*/
@Component({
  selector: 'login-component',
  templateUrl: `LoginComponent.html`,
  styleUrls  : [`LoginComponent.scss`]
})
export class LoginComponent{
  validateForm: FormGroup;
  user:User;

  constructor(private fb: FormBuilder,private localStorage:LocalStorage,private router:Router,private loginService:LoginService) {
    this.user=new User(null,null,null,null,null,null,null,null,null,null,null);
  }

  
  _submitForm() {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[ i ].markAsDirty();
    }

    this.user.account=this.user.account||"";
    this.user.password=this.user.password||"";
    

    Promise.all([
      this.loginService.getAllModulePermissionsSaveToLocalStorage(),
      this.loginService.login(this.user)])
    .then(()=>{
        this.router.navigate([ 'index' ]);
    });
  }




  ngOnInit() {
    this.user.account="admin",
    this.user.password="123456";
    this.validateForm = this.fb.group({
      account: [ null, [ Validators.required ] ],
      password: [ null, [ Validators.required ] ],
      remember: [ true ],
    });
  }
}